<?php
/**
 * Author: ThanhTV
 * Email: thanhtran@vietnambiz.com
 * Date: Web,24/08/2011
 */
class products_model extends MY_Model{

    var $table = 'products';

    function products_model()
    {
    	parent::MY_Model();
        $this->set_table($this->table);
    }

    function get_questions_subjects_info($id=0)
    {
    	$query = $this->db->where('id', $id)->get($this->table);
        if($query->num_rows() > 0) {
            $data = $query->row();
        }
        return $data;
    }    

    function del_questions_subjects($id)
    {
        return $this->db->where('id', $id)->delete($this->table);
    }

    function get_all_products($page=1 ,$limit= 20)
    {
        $this->db->select()
                 ->from($this->table)                 
                 ->limit($limit, ($page - 1)*$limit)
                 ->order_by('date_create DESC');
        $q = $this->db->get();
        $result = $q->result();
        $q->free_result();
        return $result;
    } 

    function get_count_all_products($active="")
    {
		$this->db->select("count(id) as m")->from($this->table);
		if(!empty($active))
		{
	    	$this->db->where("status",$active);
		}
        $rs = $this->db->get()->row();
        return $rs->m;
    }

    function check_code_products($code = '')
    {
        $q  =   $this->db->select()->from($this->table)->where('code',$code)->get();
        if($q->row())
        {
            return true;
        }
        else
        {
            return false;
        }
    }

    function check_title_edit($name = '',$id=0)
    {
        $where = array(
            'name'       => $name,
            'id !='      => $id
        );
        $q  =   $this->db->select()->from($this->table)->where($where)->get();
        if($q->row())
        {
            return true;
        }
        else
        {
            return false;
        }
    }   
}